/*
 * Author: Minh-Tuan Ta
 * Date: 11/6/11
 * Class: ContentTypeDB
 * This class used to access the data from ContentType table
 */
package data;

import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.sql.*;
import beans.*;
import java.util.*;

public class ContentTypeDB extends DB {
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/7/11
	 * Method: getContentTypes
	 * This method will get all the content types in the database
	 */
	public static synchronized ArrayList<ContentType> getContentTypes()
	{
		Connection conn = null;
		ArrayList<ContentType> contenttypes = new ArrayList<ContentType>();
		PreparedStatement statement = null;
		String sql = "SELECT contenttypeid, contenttypename, contenttypedescription\n" +
					 "FROM contenttype";
		
		try
		{
			conn = getConnection();
			statement = conn.prepareStatement(sql);
			ResultSet rs = statement.executeQuery();
			
			while (rs.next())
			{
				ContentType contenttype = new ContentType();
				setContentType(contenttype, rs);
				
				contenttypes.add(contenttype);
			}
			
			rs.close();
			statement.close();
			conn.close();
		}
		catch (SQLException ex)
		{
			System.out.println("Error: " + ex);
			System.out.println("Query: " + statement.toString());
		}
		
		return contenttypes;
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/7/11
	 * Method: getContentType
	 * This method will get the content type using content type id
	 */
	public static synchronized ContentType getContentType(int contenttypeid)
	{
		Connection conn = null;
		ContentType contenttype = null;
		PreparedStatement statement = null;
		String sql = "SELECT contenttypeid, contenttypename, contenttypedescription\n" +
					 "FROM contenttype\n" +
					 "WHERE contenttypeid = ?";
		
		try
		{
			conn = getConnection();
			statement = conn.prepareStatement(sql);
			statement.setInt(1, contenttypeid);
			ResultSet rs = statement.executeQuery();
			
			if (rs.next())
			{
				contenttype = new ContentType();
				setContentType(contenttype, rs);
			}
			
			rs.close();
			statement.close();
			conn.close();
		}
		catch (SQLException ex)
		{
			System.out.println("Error: " + ex);
			System.out.println("Query: " + statement.toString());
		}
		
		return contenttype;
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/7/11
	 * Method: getContentTypeID
	 * This method will get content type id of a content giving the content type name
	 */
	public static synchronized int getContentTypeID(String contenttypename)
	{
		Connection conn = null;
		ContentType contenttype = null;
		PreparedStatement statement = null;
		String sql = "SELECT contenttypeid, contenttypename, contenttypedescription\n" +
					 "FROM contenttype\n" +
					 "WHERE contenttypename = ?";
		
		try
		{
			conn = getConnection();
			statement = conn.prepareStatement(sql);
			statement.setString(1, contenttypename);
			ResultSet rs = statement.executeQuery();
			
			if (rs.next())
			{
				contenttype = new ContentType();
				setContentType(contenttype, rs);
			}
			
			rs.close();
			statement.close();
			conn.close();
		}
		catch (SQLException ex)
		{
			System.out.println("Error: " + ex);
			System.out.println("Query: " + statement.toString());
		}
		
		return contenttype.getContentTypeID();
	}
	
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/7/11
	 * Method: addContentType
	 * This method will add a new Content Type to the database
	 */
	public static synchronized int addContentType(ContentType contenttype)
	{
		Connection conn = null;
		int status = 0;
		PreparedStatement statement = null;
		String sql = "INSERT INTO contenttype(contenttypename, contenttypedescription)\n" +
					 "VALUES(?, ?)";

		try
		{
			conn = getConnection();
			statement = conn.prepareStatement(sql);
			statement.setString(1, contenttype.getContentTypeName());
			statement.setString(2, contenttype.getContentTypeDescription());
			
			status = statement.executeUpdate();
			
			statement.close();
			conn.close();
		}
		catch (SQLException ex)
		{
			System.out.println("Error: " + ex);
			System.out.println("Query: " + statement.toString());
		}
		
		return status;
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/7/11
	 * Method: updateContentType
	 * This method will update a content type
	 */
	public static synchronized int updateContentType(ContentType contenttype)
	{
		Connection conn = null;
		int status = 0;
		PreparedStatement statement = null;
		String sql = "UPDATE contenttype\n" +
					 "SET contenttypename = ?, contenttypedescription = ?\n" +
					 "WHERE contenttypeid = ?";

		try
		{
			conn = getConnection();
			statement = conn.prepareStatement(sql);
			statement.setString(1, contenttype.getContentTypeName());
			statement.setString(2, contenttype.getContentTypeDescription());
			statement.setInt(3, contenttype.getContentTypeID());
			
			status = statement.executeUpdate();
			
			statement.close();
			conn.close();
		}
		catch (SQLException ex)
		{
			System.out.println("Error: " + ex);
			System.out.println("Query: " + statement.toString());
		}
		
		return status;
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/7/11
	 * Method: deleteContentType
	 * This method will delete a content type
	 */
	public static synchronized int deleteContentType(ContentType contenttype)
	{
		Connection conn = null;
		int status = 0;
		PreparedStatement statement = null;
		String sql = "DELETE FROM contenttype\n" +
					 "WHERE contenttypeid = ?";

		try
		{
			conn = getConnection();
			statement = conn.prepareStatement(sql);
			statement.setInt(1, contenttype.getContentTypeID());
			
			status = statement.executeUpdate();
			
			statement.close();
			conn.close();
		}
		catch (SQLException ex)
		{
			System.out.println("Error: " + ex);
			System.out.println("Query: " + statement.toString());
		}
		
		return status;
	}
	
	/*
	 * Author: Minh-Tuan Ta
	 * Date: 11/7/11
	 * Method: getContentTypeMap
	 * This method will get a map of content types with their ids
	 */
	public static synchronized Map<String, Integer> getContentTypeMap()
	{
		Map<String, Integer> contentTypeMap = new HashMap<String, Integer>();
		
		Connection conn = null;
		int status = 0;
		PreparedStatement statement = null;
		String sql = "SELECT contenttypeid, contenttypename\n" +
				 	 "FROM contenttype\n";
		
		try
		{
			conn = getConnection();
			statement = conn.prepareStatement(sql);
			
			ResultSet rs = statement.executeQuery();
			
			while (rs.next())
			{
				String contentTypeName = rs.getString("contenttypename");
				int contentTypeID = rs.getInt("contenttypeid");
				
				contentTypeMap.put(contentTypeName, contentTypeID);
			}
			
			statement.close();
			conn.close();
		}
		catch (SQLException ex)
		{
			System.out.println("Error: " + ex);
			System.out.println("Query: " + statement.toString());
		}
		
		return contentTypeMap;
	}
}
